package com.example.check.controller;

import com.example.check.pojo.CheckHistory;
import com.example.check.service.CheckHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查验历史控制器
 */
@RestController
@RequestMapping("/api/check-history")
@CrossOrigin(origins = "*")
public class CheckHistoryController {
    
    @Autowired
    private CheckHistoryService checkHistoryService;
    
    /**
     * 根据ID查询查验历史
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            CheckHistory checkHistory = checkHistoryService.getById(id);
            if (checkHistory != null) {
                result.put("success", true);
                result.put("data", checkHistory);
            } else {
                result.put("success", false);
                result.put("message", "查验历史不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据行程ID查询查验历史
     */
    @GetMapping("/trip/{tripId}")
    public Map<String, Object> getByTripId(@PathVariable Integer tripId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CheckHistory> checkHistories = checkHistoryService.getByTripId(tripId);
            result.put("success", true);
            result.put("data", checkHistories);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据用户ID查询查验历史
     */
    @GetMapping("/user/{userId}")
    public Map<String, Object> getByUserId(@PathVariable Integer userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CheckHistory> checkHistories = checkHistoryService.getByUserId(userId);
            result.put("success", true);
            result.put("data", checkHistories);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据物品ID查询查验历史
     */
    @GetMapping("/item/{itemId}")
    public Map<String, Object> getByItemId(@PathVariable Integer itemId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<CheckHistory> checkHistories = checkHistoryService.getByItemId(itemId);
            result.put("success", true);
            result.put("data", checkHistories);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 添加查验历史
     */
    @PostMapping
    public Map<String, Object> add(@RequestBody CheckHistory checkHistory) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = checkHistoryService.add(checkHistory);
            if (success) {
                result.put("success", true);
                result.put("message", "添加成功");
                result.put("data", checkHistory);
            } else {
                result.put("success", false);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 更新查验历史
     */
    @PutMapping("/{id}")
    public Map<String, Object> update(@PathVariable Integer id, @RequestBody CheckHistory checkHistory) {
        Map<String, Object> result = new HashMap<>();
        try {
            checkHistory.setId(id);
            boolean success = checkHistoryService.update(checkHistory);
            if (success) {
                result.put("success", true);
                result.put("message", "更新成功");
            } else {
                result.put("success", false);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除查验历史
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = checkHistoryService.deleteById(id);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据物品ID删除查验历史
     */
    @DeleteMapping("/item/{itemId}")
    public Map<String, Object> deleteByItemId(@PathVariable Integer itemId) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = checkHistoryService.deleteByItemId(itemId);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 统计行程查验次数
     */
    @GetMapping("/trip/{tripId}/count")
    public Map<String, Object> countByTripId(@PathVariable Integer tripId) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = checkHistoryService.countByTripId(tripId);
            result.put("success", true);
            result.put("data", count);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "统计失败：" + e.getMessage());
        }
        return result;
    }
}




